﻿#include "EvilMirBody.h"
#include "../../../Shared/Globals.h"
#include "../../../Shared/Functions/Functions.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool EvilMirBody::getDragonLink() const
    {
        return _dragonlink && getEnvir()->DragonSystem != nullptr;
    }

    void EvilMirBody::setDragonLink(bool value)
    {
        if (_dragonlink == value)
        {
            return;
        }

        _dragonlink = value;
    }

    bool EvilMirBody::getCanAttack() const
    {
        return false;
    }

    bool EvilMirBody::getCanMove() const
    {
        return false;
    }

    EvilMirBody::EvilMirBody(MonsterInfo *info) : MonsterObject(info)
    {
        setDragonLink(true);
    }

    void EvilMirBody::Turn(MirDirection dir)
    {
    }

    bool EvilMirBody::Walk(MirDirection dir)
    {
        return false;
    }

    void EvilMirBody::ProcessRoam()
    {
    }

    void EvilMirBody::ProcessSearch()
    {
    }

    int EvilMirBody::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        if (getDragonLink())
        {
            if (getEnvir()->DragonSystem->LinkedMonster != nullptr)
            {
                MonsterObject *ob = getEnvir()->DragonSystem->LinkedMonster;
                if (attacker->Info->AI == 6)
                {
                    EXPOwner = nullptr;
                }

                else if (attacker->Master != nullptr)
                {
                    if (!Functions::InRange(attacker->getCurrentLocation(), attacker->Master->getCurrentLocation(), Globals::DataRange))
                    {
                        ob->EXPOwner = nullptr;
                    }
                    else
                    {

                        if (ob->EXPOwner == nullptr || ob->EXPOwner->Dead)
                        {
                            ob->EXPOwner = attacker->Master;
                        }

                        if (ob->EXPOwner == attacker->Master)
                        {
                            ob->EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
                        }
                    }

                }
            }
            getEnvir()->DragonSystem->GainExp(getEnvir()->Random->Next(1, 40));
            return 1;
        }

        return 0;
    }

    int EvilMirBody::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        if (getDragonLink())
        {
            if (getEnvir()->DragonSystem->LinkedMonster != nullptr)
            {
                MonsterObject *ob = getEnvir()->DragonSystem->LinkedMonster;
                if (ob->EXPOwner == nullptr || ob->EXPOwner->Dead)
                {
                    ob->EXPOwner = attacker;
                }

                if (ob->EXPOwner == attacker)
                {
                    ob->EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
                }
            }

            if (damageWeapon)
            {
                attacker->DamageWeapon();
            }

            getEnvir()->DragonSystem->GainExp(getEnvir()->Random->Next(1, 40));
            return 1;
        }

        return 0;
    }

    int EvilMirBody::Struck(int damage, DefenceType type)
    {
        return 0;
    }
}
